package org.colcon.web.model;

import java.util.HashMap;
import java.util.Map;

import org.colcon.web.model.framework.IGroup;
import org.colcon.web.server.Model;

public class Group extends Agent implements IGroup
{
	public String getType(){return "group";}
	
	private static final long serialVersionUID = 8534863965798154597L;
	
	protected static final String
	
		// arguments
		TEXT = "text";
	
	public static final String
		
		// containers
		LAWS = "laws",
		TITLES = "titles",
		VOTES = "votes",
		
		// titles
		MEMBER = "member",
		REPRESENTATIVE = "representative";

	public Group()
	{
		this(new HashMap<String, String>());
	}
	
	public Group(Map<String, String> args)
	{
		this(args.get(ID), args.get(TEXT), args.get(CHILDREN));
	}
	
	public Group(String id, String text, String children)
	{
		// TODO: add support for subgroups
		super("colcon/groups", id, children);
		
		setArg(TEXT, text);
	}
	
	public void setText(String text)
	{
		setArg(TEXT, text);
	}
	
	public String votes()
	{
		return getPath() + '/' + "votes";
	}
	
	public String laws()
	{
		return getPath() + '/' + "laws";
	}
	
	public String titles()
	{
		return getPath() + '/' + "titles";
	}
	
	public String getTitlePath(String title)
	{
		return titles() + '/' + title;
	}
	
	public String getLawPath(String law)
	{
		return laws() + '/' + law;
	}
	
	public String getVotePath(String vote)
	{
		return votes() + '/' + vote;
	}
	
	public static String pathToGroup(String path)
	{
		if(path == null || path.length() == 0)
			return null;
		
		String[] array = path.split("/");
		
		if(array == null || array.length < 2)
			return null;
		
		if(array[0].equals(Model.GROUPS))
			return array[1];
		
		if(array[1].equals(Model.GROUPS) && array.length > 2)
			return array[2];;
		
		return null;
	}
}
